<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="设备号" prop="mac">
      <el-input v-model="dataForm.mac" placeholder="设备号"></el-input>
    </el-form-item>
      <el-form-item label="物理宽度">
        <el-input type="number" placeholder="物理宽度" v-model="dataForm.physicsWidth">
          <template slot="append">cm</template>
        </el-input>
      </el-form-item>
      <el-form-item label="物理高度">
        <el-input type="number" placeholder="物理高度" v-model="dataForm.physicsHeight">
          <template slot="append">cm</template>
        </el-input>
      </el-form-item>
      <el-form-item label="状态" prop="switch">
        <el-radio-group v-model="dataForm.switch">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data: function () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          stbGroupId: '',
          name: '',
          mac: '',
          programId: '',
          ipaddress: '',
          screenPic: '',
          voiceSet: '',
          switch: 1,
          resolution: '',
          version: '',
          ram: '',
          physicsWidth: null,
          physicsHeight: null

        },
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ],
          stbGroupId: [
           { required: true, message: '分组id不能为空', trigger: 'blur' }
          ],
          mac: [
            {required: true, message: '设备号不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init: function (id, pid) {
        this.dataForm.id = id || 0
        this.dataForm.stbGroupId = pid
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/stb/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.stbGroupId = data.stb.stbGroupId
                this.dataForm.name = data.stb.name
                this.dataForm.mac = data.stb.mac
                this.dataForm.programId = data.stb.programId
                this.dataForm.ipaddress = data.stb.ipaddress
                this.dataForm.screenPic = data.stb.screenPic
                this.dataForm.voiceSet = data.stb.voiceSet
                this.dataForm.switch = data.stb.switchStatus
                this.dataForm.resolution = data.stb.resolution
                this.dataForm.version = data.stb.version
                this.dataForm.ram = data.stb.ram
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/stb/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'stbGroupId': this.dataForm.stbGroupId,
                'name': this.dataForm.name,
                'mac': this.dataForm.mac,
                'programId': this.dataForm.programId,
                'ipaddress': this.dataForm.ipaddress,
                'screenPic': this.dataForm.screenPic,
                'voiceSet': this.dataForm.voiceSet,
                'switchStatus': this.dataForm.switch,
                'resolution': this.dataForm.resolution,
                'version': this.dataForm.version,
                'ram': this.dataForm.ram,
                'physicsHeight': this.dataForm.physicsHeight,
                'physicsWidth': this.dataForm.physicsWidth
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
